﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace TwoPageView3
{
    public partial class MainWindow : Window
    {
        private UserPreference mUserPreference;
        private ThumbnailCache mThumbnailCache;
        private WindowOrientation mWindowOrientation;

        private string Version() {
            Assembly assembly = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            return fvi.FileVersion;
        }

        private enum State {
            TreeThumbBrowse,
            CloseupView
        }

        private State mState = State.TreeThumbBrowse;

        public MainWindow() {
            InitializeComponent();
            mUserPreference = UserPreferenceIO.LoadFromFile();
            mWindow.Title = "TwoPageView " + Version();

            InitializeTreeThumbBrowse();
            InitializeCloseupView();

            UpdateViews();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            mWindowOrientation = (Width <= Height) ? WindowOrientation.Vertical : WindowOrientation.Horizontal;

            mThumbnailCache = new ThumbnailCache();
            mThumbnailCache.LoadCachedDataFromFile(mUserPreference.ThumbnailCacheFolder);

            SetupTreeThumbBrowse();

            UpdateFlowDirection(mUserPreference.DocumentFlowDirection);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
        }

        private void Window_Closed(object sender, EventArgs e) {
            // wait until all the file writing tasks are stopped
            CancelAndWaitUntilImageDisplayTaskEnds();

            TpvPdfDirectoryInfo.RemoveTmpDirs();

            UserPreferenceIO.SaveToFile(mUserPreference);
        }

        private void ResizePanelToFillParent(Panel panel) {
            panel.Width = Double.NaN; //< Auto
            panel.Height = Double.NaN;
            panel.Margin = new Thickness(0, 0, 0, 0);
        }

        private void ResizeFrameworkElementToFillParent(FrameworkElement element) {
            element.Width = Double.NaN; //< Auto
            element.Height = Double.NaN;
        }

        private void UpdateViews() {
            Console.WriteLine("UpdateViews({0})", mState);

            switch (mState) {
            case State.TreeThumbBrowse:
                UpdateViewsTreeThumbBrowse();
                break;
            case State.CloseupView:
                UpdateViewsCloseupView();
                break;
            }
        }

        private void OnWindowKeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
            switch (mState) {
            case State.CloseupView:
                OnKeyUpOnCloseupView(e);
                break;
            case State.TreeThumbBrowse:
                OnKeyUpOnTreeThumbBrowser(e);
                break;
            }
        }

        private void UpdateFlowDirection(FlowDirection fd) {
            mThumbBrowseMenuItemRtlDocumentDirection.IsChecked = (fd == FlowDirection.RightToLeft);
            mCloseupViewMenuItemRtlDocumentDirection.IsChecked = (fd == FlowDirection.RightToLeft);
            mUserPreference.DocumentFlowDirection = fd;
            mTilePanelControl.UpdateFlowDirection(fd);
            mCloseupViewGrid.FlowDirection = fd;
        }

        private void OnToggleRtlDocumentDirection(object sender, RoutedEventArgs e) {
            UpdateFlowDirection(
                    (mUserPreference.DocumentFlowDirection == FlowDirection.LeftToRight)
                    ? FlowDirection.RightToLeft : FlowDirection.LeftToRight);
        }

        private void OnWindowSizeChanged(object sender, SizeChangedEventArgs e) {
            Console.WriteLine("OnWindowSizeChanged ({0},{1})", Width, Height);

            var newOrientation = (Width <= Height) ? WindowOrientation.Vertical : WindowOrientation.Horizontal;
            if (newOrientation != mWindowOrientation) {
                mWindowOrientation = newOrientation;
                UpdateViews();
            }
        }

        private void OnExit(object sender, RoutedEventArgs e) {
            Close();
        }
    }
}
